<?php

/**
 * KTDPlayer form.
 *
 * @package    s-test
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class KTDPlayerForm extends sfGuardUserForm
{
  public function configure()
  {
      unset($this['is_active'],
              $this['groups_list'],
              $this['permissions_list'],
              $this['last_login'],
              $this['created_at'],
              $this['updated_at'],
              $this['salt'],
              $this['algorithm'],
              $this['is_super_admin'],
              $this['username'],
              $this['password']);
      
     $this->setWidgets(array(
        'pseudo'        => new sfWidgetFormInput(array(                 "label"=> "Pseudonyme")),
        'change_password'=> new sfWidgetFormInputPassword(array(        "label"=> "Nouveau mot de passe")),
        'confirm_change_password'=> new sfWidgetFormInputPassword(array("label"=> "Confirmez le nouveau mot de passe")),
        'email_address' => new sfWidgetFormInput(array(                 "label"=> "Adresse Email")),
        'quote'         => new sfWidgetFormInput(array(                 "label"=> "Citation")),
        'avatar'        => new sfWidgetFormInput(array(                 "label"=> "Avatar")),
        'signature'     => new sfWidgetFormKTDTextarea(array(              "label"=> "Signature", "hrp_mode"=> true))

     ));
     $this->setValidators(array(
         'pseudo'   => new sfValidatorString(array('required' => true)),
         'email_address' => new sfValidatorEmail(array('required' => true)),
         'avatar'   => new sfValidatorUrl(array('required' => false)),
         'quote'   => new sfValidatorString(array('required' => false)),
         'signature'   => new sfValidatorString(array('required' => false)),
         'change_password'   => new sfValidatorString(array('required' => false)),
         'confirm_change_password'   => new sfValidatorString(array('required' => false)),
     ));
     $this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('change_password', sfValidatorSchemaCompare::EQUAL, 'confirm_change_password'));
     $this->widgetSchema->setNameFormat("ktd_player[%s]");
  }
  
  public function updateObject($values = NULL){
      parent::updateObject($values);
      $values = $this->getValues();

      //on change le mot de passe si c'est souhaité.
       if(!empty($values['change_password']))
              $this->getObject()->setPassword($this->getValue('change_password'));

      return $this->object;
  }
}
class BackendKTDPlayerForm extends sfGuardUserForm{
  public function configure()
  {
      unset(  $this['last_login'],
              $this['created_at'],
              $this['updated_at'],
              $this['is_super_admin']
            );
      $this->setWidgets(array(
          "username"    => new sfWidgetFormInput(array(
              "label"   => "Nom d'utilisateur"
          )),
          "pseudo"      => new sfWidgetFormInput(array(
              "label"   => "Pseudonyme"
          )),
          "change_password"    => new sfWidgetFormInput(array(
              "label"   => "Nouveau mot de passe"
          )),
          "email_address"=> new sfWidgetFormInput(array(
              "label"   => "Email"
          )),
          "is_active"   => new sfWidgetFormInputCheckbox(array(
              "label"   => "Est activé"
          )),
          "msg_count"   => new sfWidgetFormInput(array(
              "label"   => "Nombre de messages"
          )),
          "money"       => new sfWidgetFormInput(array(
              "label"   => "Quantité d'argent"
          )),
          "groups_list" => new sfWidgetFormDoctrineChoice(array(
              "model" => "sfGuardGroup",
              "multiple"=> true,
              "label" => "Groupes utilisateurs"
          ))
      ));

      $this->setValidators(array(
          "username"    =>  new sfValidatorString(array(
              "required"=> true
          )),
          "pseudo"      =>  new sfValidatorString(array(
              "required"=> true
          )),
          "change_password"    =>  new sfValidatorString(array(
              "required"=> false
          )),
          "is_active"   =>  new sfValidatorBoolean(),
          "email_address"=> new sfValidatorEmail(array(
              "required" => true
          )),
          "msg_count"   => new sfValidatorInteger(array("required"=>true)),
          "money"   => new sfValidatorInteger(array("required"=>true)),
          "groups_list" => new sfValidatorDoctrineMultipleChoice(array("model" => "sfGuardGroup", "required"=>true))

      ));
      $this->widgetSchema->setNameFormat("ktd_player[%s]");
  }
    public function updateObject($values = NULL){
      parent::updateObject($values);
      $values = $this->getValues();

      //on change le mot de passe si c'est souhaité.
       if(!empty($values['change_password']))
              $this->getObject()->setPassword($this->getValue('change_password'));

      return $this->object;
  }
}